#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
using ll = long long;
const int mod = 998244353;

int a[N][10]; // 存储骰子面数
ll p[N];      // 记录每个骰子当前<=x的数字个数
ll qpow(ll a, ll b) {
  ll res = 1;
  for (a %= mod; b; b >>= 1, a = a * a % mod)
    if (b & 1)
      res = res * a % mod;
  return res;
}

int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> nums; // 存储(数字,骰子ID)

  // 读取输入并初始化
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= 6; j++) {
      cin >> a[i][j];
      nums.emplace_back(a[i][j], i);
    }

  sort(nums.begin(), nums.end());

  ll tot = 1, res = 0, last = 0;
  ll inv_base = qpow(qpow(6, n), mod - 2); // 1/6^n的逆元
  int active_dice = 0;                     // 已激活的骰子数

  for (int i = 0; i < nums.size();) {
    int j = i, x = nums[i].first;

    // 处理相同数字
    while (j < nums.size() && nums[j].first == x) {
      int id = nums[j].second;
      if (p[id])
        tot = tot * qpow(p[id], mod - 2) % mod;
      else
        active_dice++;
      tot = tot * ++p[id] % mod;
      j++;
    }

    // 计算当前数字的贡献
    if (active_dice == n) {
      ll prob = (tot - last + mod) % mod;
      res = (res + x * prob % mod * inv_base) % mod;
      last = tot;
    }

    i = j; // 跳过已处理数字
  }

  cout << res << endl;
  return 0;
}